package com.zhudoo.zhilian.util.web;

import com.alibaba.fastjson.JSON;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 返回的工具类 所有的提示配置都必须写在这里
 * Created by 王念 on 2016/11/14.
 *
 * @author wangnian
 */
public class ResponseUtil {

    private static Logger logger = LoggerFactory.getLogger(ResponseUtil.class);

    /**
     * 成功
     */
    public static String SUCCESS = "SUCCESS";
    /**
     * 失败
     */
    public static String FAILURE = "FAILURE";

    /**
     * 无效的账号
     */
    public static String INVALID = "INVALID";

    /**
     * 没绑定手机号码
     */
    public static String NOT_PHONE = "NOT_PHONE";

    /**
     * 没有实名认证
     */
    public static String NOT_CERTIFICATION = "NOT_CERTIFICATION";

    /**
     * 基本方法
     *
     * @param status
     * @param msg
     * @param body
     * @return
     */
    private static Response response(String status, String msg, Object body) {
        return new Response(status, msg, body);
    }


    /**
     * 返回成功
     *
     * @param msg
     * @return
     */
    public static Response responseSuccess(String msg) {
        logger.info("接口响应消息:{}", msg);
        return response(SUCCESS, msg, null);
    }

    /**
     * 返回成功
     *
     * @param msg
     * @param body
     * @return
     */
    public static Response responseSuccess(String msg, Object body) {
        if (body instanceof List) {
            List list = (List) body;
            if (list.size() > 0) {
                logger.info("接口响应List数量:{},第一个数据:{}，消息:{}", list.size(), JSON.toJSON(list.get(0)), msg);
            } else {
                logger.info("接口响应List数量:{}，消息:{}", 0, msg);
            }
        } else {
            logger.info("接口响应数据:{}", JSON.toJSON(body), msg);
        }
        return response(SUCCESS, msg, body);
    }

    /**
     * 复杂类型
     *
     * @param msg
     * @param list
     * @param data
     * @return
     */
    public static Response responseSuccess(String msg, List list, Object data) {
        Map<String, Object> body = new HashMap(2);
        if (list.size() > 0) {
            logger.info("接口响应List数量:{},第一个数据:{}", list.size(), JSON.toJSON(list.get(0)));
        } else {
            logger.info("接口响应List数量:{}", 0);
        }
        logger.info("响应的data数据:{}", data);
        body.put("list", list);
        body.put("data", data);
        return response(SUCCESS, msg, body);
    }

    /**
     * list类型
     *
     * @param msg
     * @param list
     * @param page
     * @param data
     * @return
     */
    public static Response responseSuccess(String msg, List list, Object page, Object data) {
        Map<String, Object> body = new HashMap(2);
        if (list.size() > 0) {
            logger.info("接口响应List数量:{},第一个数据:{}", list.size(), JSON.toJSON(list.get(0)));
        } else {
            logger.info("接口响应List数量:{}", 0);
        }
        logger.info("响应的data数据:{}", data);
        body.put("list", list);
        body.put("data", data);
        body.put("page", page);
        return response(SUCCESS, msg, body);
    }

    /**
     * 返回失败
     *
     * @param msg
     * @return
     */
    public static Response responseFailure(String msg) {
        logger.info("失败，提示消息:{}", msg);
        return response(FAILURE, msg, null);
    }

    /**
     * 返回绑定
     *
     * @param msg
     * @return
     */
    public static Response responseNotPhone(String msg) {
        logger.info("没绑定手机号码，提示消息:{}", msg);
        return response(NOT_PHONE, msg, null);
    }

    /**
     * 返回没有实名认证
     *
     * @param msg
     * @return
     */
    public static Response responseNotCertification(String msg) {
        logger.info("没有实名认证，提示消息:{}", msg);
        return response(NOT_CERTIFICATION, msg, null);
    }


}
